/**
 * @author: ferret
 * @date: 2024/8/17
 */
import * as NaiveUI from 'naive-ui';
import {ref, computed, App} from 'vue';

const configProviderPropsRef = computed(() => ({}));

export const Message = ref();

export const Dialog = ref();

export const Notification = ref();

export const LoadingBar = ref();

export const Modal = ref();

export const initConfigProvider = () => {
  const {message, dialog, notification, loadingBar, modal} = NaiveUI.createDiscreteApi(
    ['message', 'dialog', 'notification', 'loadingBar', 'modal'],
    {
      configProviderProps: configProviderPropsRef,
    }
  );
  // console.log('initConfigProvider >>> message=', message);
  Message.value = message;
  Dialog.value = dialog;
  Notification.value = notification;
  LoadingBar.value = loadingBar;
  Modal.value = modal;
};

/**
 * 销毁所有的消息
 */
export const destroyMessage = () => {
  Message.value.destroyAll();
};

/**
 * 提示成功消息
 * @param content
 */
export const successMessage = (content) => {
  Message.value.success(content, {closable: true});
};

/**
 * 提示错误消息
 * @param content
 */
export const errorMessage = (content) => {
  Message.value.error(content, {closable: true});
};

/**
 * 显示加载条
 */
export const startLoadingBar = () => {
  LoadingBar.value.start();
};

/**
 * 结束加载条
 */
export const finishLoadingBar = () => {
  LoadingBar.value.finish();
};

const ApiInstall = {
  install(app: App) {
    initConfigProvider();
  }
};

export default ApiInstall;
